<template>
  <div class="app-container">
    <el-form :inline="true" @submit.native.prevent>
      <el-form-item>
        <el-input v-model="listContext.filter" :placeholder="$t('AbpIdentity[\'Search\']')" @keyup.enter.native="fetchData" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="fetchData">
          <i class="el-icon-search" />
        </el-button>
      </el-form-item>
      <el-button
        type="primary"
        style="float: right"
        @click="openCreateRoleDialog"
      >
        <i class="el-icon-plus" />{{ $t('AbpIdentity[\'NewRole\']') }}
      </el-button>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="$t('AbpIdentity[\'LoadingWithThreeDot\']')"
      :empty-text="$t('AbpIdentity[\'NoDataAvailableInDatatable\']')"
      border
      fit
      highlight-current-row
      height="calc(100vh - 225px)"
      @sort-change="sortChange"
    >
      <el-table-column :label="$t('AbpIdentity[\'RoleName\']')" prop="name" sortable="custom">
        <template slot-scope="scope">
          {{ scope.row.name }}
          <el-tag v-show="scope.row.isDefault">{{ $t('AbpIdentity[\'DisplayName:IsDefault\']') }}</el-tag>
          <el-tag v-show="scope.row.isStatic">{{ $t('AbpIdentity[\'DisplayName:IsStatic\']') }}</el-tag>
          <el-tag v-show="scope.row.isPublic">{{ $t('AbpIdentity[\'DisplayName:IsPublic\']') }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t('AbpIdentity[\'Actions\']')">
        <template slot-scope="scope">
          <el-dropdown trigger="click" @command="clickActionDropDown">
            <el-button type="primary">
              <i class="el-icon-setting" />
              {{ $t('AbpIdentity[\'Actions\']') }}
              <i class="el-icon-arrow-down" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item :command="{ action: 'edit', row: scope.row }">{{ $t('AbpIdentity[\'Edit\']') }}</el-dropdown-item>
              <!--              <el-dropdown-item :command="{ action: 'claim', row: scope.row }">{{ $t('AbpIdentity[\'Claims\']') }}</el-dropdown-item>-->
              <el-dropdown-item :command="{ action: 'permission', row: scope.row }">{{ $t('AbpIdentity[\'Permissions\']') }}</el-dropdown-item>
              <el-dropdown-item v-show="!scope.row.isStatic" :command="{ action: 'delete', row: scope.row }">{{ $t('AbpIdentity[\'Delete\']') }}</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <div class="block" style="margin-top: 10px">
      <el-pagination
        :page-sizes="pageContext.pageSizes"
        :page-size.sync="listContext.maxResultCount"
        :current-page.sync="pageContext.currentPage"
        :total="pageContext.totalCount"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="fetchData"
        @current-change="handleCurrentChange"
      />
    </div>
    <create-or-edit-role ref="createOrEditRole" :visible.sync="roleDialogVisible" :context="roleDialogCtx" @saveSucceed="fetchData" />
    <user-or-role-permission ref="userOrRolePermission" :visible.sync="permissionDialogVisible" :context="permissionDialogCtx" />
  </div>
</template>

<script>
import { getList, deleteById } from '@/api/administration/role/role'
import CreateOrEditRole from '@/views/administration/identity/role/components/createOrEditRole'
import UserOrRolePermission from '@/views/administration/identity/components/userOrRolePermission'

export default {
  name: 'Role',
  components: { UserOrRolePermission, CreateOrEditRole },
  data() {
    return {
      list: null,
      listLoading: false,
      pageContext: {
        pageSizes: [10, 25, 50, 100],
        totalCount: 0,
        currentPage: 1
      },
      listContext: {
        filter: '',
        sorting: '',
        skipCount: 0,
        maxResultCount: 10
      },
      roleDialogCtx: {
        id: '',
        title: '',
        actionMode: ''
      },
      roleDialogVisible: false,
      permissionDialogCtx: {
        providerName: '',
        providerKey: ''
      },
      permissionDialogVisible: false
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getList(this.listContext).then((response) => {
        this.list = response.items
        this.pageContext.totalCount = response.totalCount
        this.listLoading = false
      })
    },
    handleCurrentChange(currentPage) {
      this.listContext.skipCount = (currentPage - 1) * this.listContext.maxResultCount
      this.fetchData()
    },
    sortChange({ prop, order }) {
      this.listContext.sorting = ''
      if (order) {
        this.listContext.sorting = prop + ' ' + order
      }
      this.fetchData()
    },
    clickActionDropDown(ctx) {
      switch (ctx.action) {
        case 'edit':
          this.openEditRoleDialog(ctx.row.id)
          break
        case 'permission':
          this.permissionDialogVisible = true
          this.permissionDialogCtx = {
            providerName: 'R',
            providerKey: ctx.row.name
          }
          break
        case 'delete':
          this.$confirm(this.$t('AbpIdentity[\'AreYouSure\']')).then(async _ => {
            await deleteById(ctx.row.id)
            this.$message({
              message: this.$t('AbpIdentity[\'Ok\']'),
              type: 'success'
            })
            this.fetchData()
          }).catch(_ => {})
          break
      }
    },
    openCreateRoleDialog() {
      this.roleDialogVisible = true
      this.roleDialogCtx = {
        title: this.$t('AbpIdentity[\'NewRole\']'),
        actionMode: 'create'
      }
    },
    openEditRoleDialog(id) {
      this.roleDialogVisible = true
      this.roleDialogCtx = {
        title: this.$t('AbpIdentity[\'Edit\']'),
        actionMode: 'edit',
        id: id
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>

